package com.pp.small.product.controller;

import com.pp.small.common.base.BaseResult;
import com.pp.small.product.entity.Product;
import com.pp.small.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * spu信息 前端控制器
 * </p>
 *
 * @author pzh
 * @since 2024-12-12 16:12:35
 */
@Tag(name = "产品", description = "产品相关接口")
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/loadProduct")
    @Operation(summary = "载入商品" , description = "载入商品")
    public BaseResult<Product> loadProduct(@RequestBody Product product) {
        log.info("载入商品[{}]", product);

        productService.saveProduct(product);
        return BaseResult.success(product);
    }

    @GetMapping("/listProduct")
    @Operation(summary = "获取所有商品" , description = "获取所有商品")
    public BaseResult<List<Product>> listProduct() {
        log.info("获取所有商品");

        List<Product> spus = productService.listProduct();
        return BaseResult.success(spus);
    }

    @GetMapping("/listProductByName")
    @Operation(summary = "根据名字获取商品" , description = "根据名字获取商品")
    public BaseResult<List<Product>> listProductByName(@RequestParam String name) {
        log.info("根据名字[{}]获取商品", name);

        List<Product> spus = productService.listByName(name);
        return BaseResult.success(spus);
    }

    @GetMapping("/getProduct")
    @Operation(summary = "根据ID获取商品" , description = "根据ID获取商品")
    public BaseResult<Product> getProduct(@RequestParam String id) {
        log.info("根据ID[{}]获取商品", id);

        Product product = productService.getProduct(id);
        return BaseResult.success(product);
    }
}
